﻿/*
版权所有：版权所有(C) 2010，刘建新
文件编号：M01_UsermenuBll.cs
文件名称：UsermenuBll.cs
系统编号：Z0003007
系统名称：通用办公系统
模块编号：M01
模块名称：信息发布
设计文档：IOA_M01信息发布XDE建模.mdx
完成日期：2010/12/17
作　　者：
内容摘要：表[UserMenu]业务操作类
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Eis51.CampanyCMS.Model.Bus;
using System.Data;
using Eis51.BuildingHtml;
using System.Web.UI.WebControls;
using Eis51.CampanyCMS.Model.NewsManager.QueryO;
using Eis51.CampanyCMS.Business.NewsManager;

namespace Eis51.CampanyCMS.Business.Bus
{
    /// <summary>
	/// Usermenu的BLL 自定义方法
	/// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
	/// </summary>
    public partial class UsermenuBLL : BaseBLL<Usermenu>
    {
        public DataTable GetDataByUserName(string Username)
        {
            string sql = string.Format("select * from UserMenu where CreateBy='{0}' order by parentid,id  ", Username);

            return this.GetDataTable(sql);
        }

        public DataTable GetUserNamelist()
        {
            string sql = "select distinct User_Name as CreateBy from Administrator ";

            return this.GetDataTable(sql);
        }

        public void DeleteMyMenu(string Username)
        {
            string sql = "delete  from UserMenu  where CreateBy ='" + Username + "'";
            this.GetDataTable(sql);
        }

        public void CopyMenu(string strFrom, string strTo)
        {
            DataTable dt = GetDataByUserName(strFrom);
            DataRow[] ParentArr = dt.Select("ParentID=0");
            foreach (DataRow dr in ParentArr)
            {
                int Parentid = 0;
                CopyInsert(dr, strTo, ref Parentid);
                DataRow[] Arr1 = dt.Select("ParentID=" + dr["SEQNO"].ToString());
                if (Arr1 != null && Arr1.Length > 0)
                {
                    foreach (DataRow dr1 in Arr1)
                    {
                        dr1["ParentID"] = Parentid.ToString();
                        TreeInsert(dr1, dt, strTo);
                    }
                }
            }
        }

        private void TreeInsert(DataRow dr, DataTable dt, string strTo)
        {
            int Parentid = 0;
            CopyInsert(dr, strTo, ref Parentid);
            DataRow[] Arr1 = dt.Select("ParentID=" + dr["SEQNO"].ToString());
            if (Arr1 != null && Arr1.Length > 0)
            {
                foreach (DataRow dr1 in Arr1)
                {
                    dr1["ParentID"] = Parentid.ToString();
                    TreeInsert(dr1, dt, strTo);
                }
            }
        }

        private void CopyInsert(DataRow dr, string strTo, ref int parentid)
        {
            Usermenu u = Usermenu.Dr2Obj(dr, Usermenu.ColumnNameEnum.DBName);
            u.Createby = strTo;
            parentid = this.Insert(u);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="listControl"></param>
        public void BindDropListTreeType(ListControl listControl, string CreateBy)
        {
            string sql = string.Format("select * from UserMenu where CreateBy='{0}' ", CreateBy);
            DataTable dt = this.GetDataTable(sql);
            DataTable dtNew = new DataTable();
            dtNew.Columns.Add("ParentID");
            dtNew.Columns.Add("TITLE");
            dtNew.Columns.Add("SEQNO");

            TreeListData("0", dt, dtNew);
            if (dtNew == null || dtNew.Rows.Count <= 0)
            {
                listControl.DataSource = dt;
            }
            else
            {
                listControl.DataSource = dtNew;
            }

            listControl.DataTextField = "TITLE";
            listControl.DataValueField = "SEQNO";
            listControl.DataBind();

        }
        string symbolf = "";
        string symbol = "";
        public void TreeListData(string parentID, DataTable allTable, DataTable NewTable)
        {
            DataRow[] dataArr = allTable.Select("ParentID=" + parentID);
            int total = dataArr.Length;
            for (int i = 0; i < total; i++)
            {
                if ((i + 1) == total)
                    symbol = "└";
                else
                    symbol = "├";
                DataRow newRow = NewTable.NewRow();
                newRow["ParentID"] = dataArr[i]["ParentID"].ToString();
                newRow["TITLE"] = symbolf + symbol + dataArr[i]["TITLE"].ToString();
                newRow["SEQNO"] = dataArr[i]["SEQNO"].ToString();
                NewTable.Rows.Add(newRow);
                if ((i + 1) == total)
                    symbolf += "　";
                else
                    symbolf += "│";
                TreeListData(dataArr[i]["SEQNO"].ToString(), allTable, NewTable);
                symbolf = symbolf.Substring(0, symbolf.Length - 1);
            }
        }


        public void SaveMenu(NewsTypeQuery query)
        {
            string Username = query.CreateBy;
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> \n <NavMenu> \n  {0} \n  </NavMenu> ";
            string TopXml = "<TopMenu Text=\"|TITLE|\" ID=\"|SEQNO|\"  Defualt=\"|ISDEFAULT|\"  BackGroupClass=\"|MY_CLASS|\"  NavigateUrl=\"~/admin/Frame/leftmenu.aspx\"> {0}  \n  </TopMenu> ";
            string SendXml = "<Menus Text=\"|TITLE|\" ID=\"|SEQNO|\"  NavigateUrl=\"|NAVIGATEURL|\" > {0}  \n  </Menus> ";
            string TreeXml = "<MenuItem Text=\"|TITLE|\" ID=\"|SEQNO|\"  NavigateUrl=\"|NAVIGATEURL|\" ></MenuItem>  \n ";
            string Rvalue = "";
            DataTable dt = GetDataByUserName(Username);
            DataRow[] dataArr = dt.Select("ParentID=0");
            string rvalue1 = "";
            string rvalue2 = "";
            string rvalue3 = "";
            int n = 1;
            foreach (DataRow dr1 in dataArr)
            {
                rvalue2 = "";
                DataRow[] dataArr2 = dt.Select("ParentID=" + dr1["Seqno"].ToString());
                if (dataArr2 != null && dataArr2.Length > 0)
                {
                    foreach (DataRow dr2 in dataArr2)
                    {
                        DataRow[] dataArr3 = dt.Select("ParentID=" + dr2["Seqno"].ToString());
                        rvalue3 = "";
                        if (dataArr3 != null && dataArr3.Length > 0)
                        {
                            foreach (DataRow dr3 in dataArr3)
                            {
                                rvalue3 += strReplace(dr3, TreeXml);
                            }
                        }
                        rvalue2 += strReplace(dr2, SendXml);
                        rvalue2 = string.Format(rvalue2, rvalue3);
                    }
                }
                //n==1时候，聚焦点
                if (n == 1)
                {
                    rvalue1 += strReplace(dr1, TopXml.Replace("|SEQNO|", "1"));
                    rvalue1 = rvalue1.Replace("|MY_CLASS|", "nw");
                }
                else
                {
                    rvalue1 += strReplace(dr1, TopXml);
                    rvalue1 = rvalue1.Replace("|MY_CLASS|", "cen");
                }
                n++;
                rvalue1 = string.Format(rvalue1, rvalue2);
            }
            rvalue1 += NewTypeMenu(query);
            xml = string.Format(xml, rvalue1);
            BuildCommon.CreaetHTMLFile("~/Admin/Frame/" + query.CreateBy + "Menu.xml", xml, Encoding.UTF8);
        }

        public string NewTypeMenu(NewsTypeQuery query)
        {

            string TopXml = "<TopMenu Text=\"内容设置\" ID=\"10000001\"  BackGroupClass=\"ne\"  NavigateUrl=\"~/admin/Frame/leftmenu.aspx\"> {0}  \n </TopMenu> ";
            string SendXml = "<Menus Text=\"|TITLE|\" ID=\"Menus|SEQNO|\"  NavigateUrl=\"|URL|\"   > {0}  \n </Menus> ";
            string TreeXml = "<MenuItem Text=\"|TITLE|\" ID=\"MenuItem|SEQNO|\"  NavigateUrl=\"|URL|\" ></MenuItem>  \n";
            string rvalue1 = "";
            string rvalue2 = "";
            DataTable dt = NewstypeBLL.GetInstance().GetTypeByUsername(query);
            DataRow[] dataArr = dt.Select("ParentID=0");
            foreach (DataRow dr1 in dataArr)
            {
                rvalue2 = "";
                DataRow[] dataArr2 = dt.Select("ParentID=" + dr1["SEQNO"].ToString());
                if (dataArr2 != null && dataArr2.Length > 0)
                {
                    foreach (DataRow dr2 in dataArr2)
                    {
                        rvalue2 += strReplace(dr2, TreeXml).Replace("|URL|", GetTypeUrl(dr2));
                    }
                }
                string temp = "";
                if (rvalue2 == string.Empty)
                {
                    temp = strReplace(dr1, SendXml).Replace("|URL|", GetTypeUrl(dr1));
                    rvalue2 = string.Format(temp, "");
                }
                else
                {
                    temp = strReplace(dr1, SendXml).Replace("|URL|", "");
                    rvalue2 = string.Format(temp, rvalue2);
                }


                rvalue1 += rvalue2;
            }
            TopXml = string.Format(TopXml, rvalue1);

            return TopXml;
        }

        public string GetTypeUrl(DataRow dr)
        {
            string url = "~/admin/NewsManager/NewsList.aspx?Typecode={0}";
            return string.Format(url, dr["Code"].ToString());
        }

        private string strReplace(DataRow dr, string strText)
        {
            string txt = strText;
            foreach (DataColumn col in dr.Table.Columns)
            {
                txt = txt.Replace("|" + col.ColumnName.ToUpper() + "|", dr[col.ColumnName].ToString());
            }
            return txt;

        }
   
    }


    #region 代码自动生成
    /// <summary>
    /// Usermenu的BLL
    /// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
    /// </summary>
    public partial class UsermenuBLL : BaseBLL<Usermenu>
    {
        #region 自动生成代码：取得相关实体

        /// <summary>
        /// 方法名称: UsermenuBLL
        /// 内容摘要: 构造函数进行初始化
        /// </summary>
        protected UsermenuBLL()
        {
        }
        private static volatile UsermenuBLL m_instance = null;
        /// <summary>
        /// 方法名称: GetInstance
        /// 内容摘要: 取得 UsermenuBLL 对象
        /// </summary>
        /// <returns>返回一个已经存在的实体</returns>
        public static UsermenuBLL GetInstance()
        {
            // 通用的必要代码 iBatisNet双校检机制,如果实例不存在
            if (m_instance == null)
            {
                lock (typeof(UsermenuBLL))
                {

                    // 如果实例不存在
                    if (m_instance == null)
                    {

                        // 创建一个的实例
                        m_instance = new UsermenuBLL();
                    }
                }
            }

            // 返回业务逻辑对象
            return m_instance;
        }

        #endregion

        #region 自动生成代码：基本业务公开函数

        /// <summary>
        /// 方法名称: Get
        /// 内容摘要: 由键值获取一个实体对象
        /// </summary>
        /// <returns>Usermenu</returns>
        public Usermenu Get(Usermenu.Key key)
        {
            return null;
        }
        /// <summary>
        /// 方法名称: Select
        /// 内容摘要: 基本查询，不带任何条件的查询
        /// </summary>
        public IList<Usermenu> Select()
        {
            return null;
        }
        /// <summary>
        /// 方法名称: Insert
        /// 内容摘要: 插入一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Insert(Usermenu obj)
        {
            int result = 0;
            try
            {
                return 0;
            }
            catch (Exception e)
            {
                throw e;
            }
            return result;
        }

        /// <summary>
        /// 方法名称: Update
        /// 内容摘要: 更新一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Update(Usermenu obj)
        {
            int result = 0;
            return result;
        }

        /// <summary>
        /// 方法名称: Delete
        /// 内容摘要: 删除一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Delete(Usermenu obj)
        {
            int result = 0;
            return result;
        }

        #endregion

    }
    #endregion
}